extends PanelContainer

var item = ""     #当前原料槽存放物品
@onready var bag = $"../../../.."

#清空函数，用于取回/合成物品/退出背包
func clear_ingredient_sprite():
	for sprite in get_children():
		sprite.queue_free()
		#当前原料槽清空物品
		item = ""
				

func _ready():
	mouse_filter = Control.MOUSE_FILTER_PASS
	gui_input.connect(_on_gui_input)

func _on_gui_input(event) -> void:
	if event is InputEventMouseButton:
		if event.button_index == MOUSE_BUTTON_LEFT and event.pressed:
			SeManager.play_ingre()
			if GameUtiles.current_item != "item0" \
			and GameUtiles.ingredients.size() < 3\
			and GameUtiles.current_item != "" \
			and item == "":            #选中非空，当前原料槽为空：向原料槽填充物品
				
				item = GameUtiles.current_item #当前原料槽同步物品
				#ingre列表中添加当前物品
				GameUtiles.ingredients.append(item)
				#背包移除当前物品1个
				GameUtiles.player_bag[item] -= 1
				
				#可视化
				var sprite=Sprite2D.new()
				sprite.texture = load("res://images/assets/"+str(item)+".png")
				sprite.position = Vector2(16,16)
				sprite.scale = Vector2(0.8,0.8)
				add_child(sprite)
				
				
				
				
			if GameUtiles.current_item == "item0" \
			and GameUtiles.ingredients.size() <= 3\
			and GameUtiles.current_item != "" \
			and item != "":                #槽内存在物品，取回
				#背包增加当前物品1个
				GameUtiles.player_bag[item] += 1
				#ingre列表中移除当前物品
				GameUtiles.ingredients.erase(item)
				#去可视化
				clear_ingredient_sprite()
				
				
			#当前处理item归零
			GameUtiles.current_item="item0"
			#重新填充背包
			bag.fill_in_bag()	
			
